package demo1;

import java.io.File;
import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-10-07
 * Time: 15:31
 */
public class Test {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {

        System.out.println("请输入要查找的跟目录：");
        File rootpath = new File(scanner.nextLine());
        System.out.println("请输入要查找的关键词：");
        String word = scanner.nextLine();
        if(!rootpath.isDirectory()){
            System.out.println("该根目录不合法");
            return;
        }
        scanDir(rootpath, word);
    }
    public static void scanDir(File rootpath, String word){
        File[] files = rootpath.listFiles();
        if(files == null || files.length == 0){
            return;
        }
        for (int i = 0; i < files.length; i++) {
            if(files[i].isDirectory()){
                scanDir(files[i], word);
            }else{
                delect(files[i], word);
            }
        }
    }
    public static void delect(File rootpath, String word){
        System.out.println(rootpath.getName());
        if(!rootpath.getName().contains(word)){
            return;
        }
        System.out.println("确认删除该文件（Y/N）");
        String ch = scanner.next();
        if(ch.equals("Y")){
            rootpath.delete();
            System.out.println("删除成功");
        }else if(ch.equals("N")){
            ;
        }else {
            System.out.println("输入违法");
        }
    }
}
